package com.tz.farmer.config;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


/**
 * @description: 集成RedisTemplate
 * @name RedisConfig
 * @author: xuhj
 * @date: 2020/6/18
 * @version V1.0
 */

public class RedisConfig {

//    //Redis服务器IP
//    private static String ADDR = "192.168.0.64";
//
//    //Redis的端口号
//    private static int PORT = 6379;


    //Redis服务器IP
    private static String ADDR = "111.3.64.45";

    //Redis的端口号
    private static int PORT = 6379;

    private static String AUTH = "luct@123";


    //可用连接实例的最大数目，默认值为8；
    private static int MAX_TOTAL = 512;

    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
    private static int MAX_IDLE = 512;

    //等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。
    private static int MAX_WAIT = 10000;

    private static int TIMEOUT = 10000;

    //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

    /**
     * @description:  初始化Redis连接池
     * @name static initializer
     * @author: xuhj
     * @date: 2019/3/6 20:51
     * @version V1.0
     * @param null:
     * @return: null
     */
    static {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_TOTAL);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT,AUTH);
//            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @description:  获取Jedis实例
     * @name getJedis
     * @author: xuhj
     * @date: 2019/3/6 20:52
     * @version V1.0

     * @return: redis.clients.jedis.Jedis
     */
    public static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis jedis = jedisPool.getResource();
                return jedis;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description:  释放Jedis实例
     * @name returnJedis
     * @author: xuhj
     * @date: 2019/3/6 20:52
     * @version V1.0
     * @param jedis:
     * @return: void
     */
    public static void returnJedis(Jedis jedis) {
        if (jedisPool != null) {
            jedisPool.returnResource(jedis);
        }
    }

    /**
     * @description:  从list中获得数据(先进先出)(移除并返回列表 key 的头元素)
     * @name pop_list
     * @author: wll
     * @date: 2019/3/21 11:45
     * @version V1.0
     * @param list_nm:
     * @return: java.lang.String
     */
    public static String rpop(String list_nm) {
        String rtn = "";
        Jedis jedis = null;
        try {
            jedis = getJedis();
            rtn = jedis.rpop(list_nm);
        }finally {
            returnJedis(jedis);
        }
        return rtn;
    }
}